package com.threeti.mecool.core.domain.model.plan.pojo;

import com.threeti.mecool.core.application.basicdata.BasicDataService;
import com.threeti.mecool.core.domain.model.basicdata.biz.Channel;
import com.threeti.mecool.core.infrastructure.persistence.plan.PlanJobRepository;
import org.codehaus.jackson.annotate.JsonBackReference;
import org.codehaus.jackson.map.annotate.JsonDeserialize;
import org.springframework.beans.factory.annotation.Configurable;

import javax.annotation.Resource;
import javax.persistence.*;
import java.util.Date;

@Configurable
@Entity
@Table(name = "DM_PLAN_JOB")
public class PlanJob {
    private Long id;
    private Integer version;
    private PlanProject planProject;
    private String storeCode;
    private String storeName;
    private String storeAddress;
    private Job job;
    private Long jobId;
    private Date startTime;
    private Date endTime;
    private int priority;
    private BasicDataService basicDataService;
    private PlanJobRepository repository;

    public static PlanJob findById(Long id) {
        return repository().findOne(id);
    }

    //<editor-fold desc="get/set">
    private static PlanJobRepository repository() {
        return new PlanJob().repository;
    }

    @Resource
    public void setBasicDataService(BasicDataService basicDataService) {
        this.basicDataService = basicDataService;
    }

    @Resource
    public void setRepository(PlanJobRepository repository) {
        this.repository = repository;
    }

    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }

    @Version
    public Integer getVersion() {
        return version;
    }

    @JsonBackReference("planJobs")
    @ManyToOne
    public PlanProject getPlanProject() {
        return planProject;
    }

    public String getStoreCode() {
        return storeCode;
    }

    @Transient
    public String getStoreName() {
        if (storeName == null && storeCode != null) {
            Channel channel = null;
            try {
                channel = basicDataService.findChannelBy(storeCode);
            } catch (Exception ignore) {
            }
            if (channel != null) {
                storeName = channel.getName();
            }
        }
        return storeName;
    }

    @Transient
    public String getStoreAddress() {
        if (storeAddress == null && storeCode != null) {
            Channel channel = null;
            try {
                channel = basicDataService.findChannelBy(storeCode);
            } catch (Exception ignore) {
            }
            if (channel != null) {
                storeAddress = channel.getAddress();
            }
        }
        return storeAddress;
    }

    @ManyToOne
    public Job getJob() {
        if (job == null && jobId != null) {
            job = Job.findById(jobId);
        }
        return job;
    }

    @Transient
    public Long getJobId() {
        if (jobId == null && job != null) {
            jobId = job.getId();
        }
        return jobId;
    }

    @JsonDeserialize(using = SqlTimeDeserializer.class)
    @Temporal(TemporalType.TIME)
    public Date getStartTime() {
        return startTime;
    }

    @JsonDeserialize(using = SqlTimeDeserializer.class)
    @Temporal(TemporalType.TIME)
    public Date getEndTime() {
        return endTime;
    }

    public int getPriority() {
        return priority;
    }

    public void setStoreAddress(String storeAddress) {
        this.storeAddress = storeAddress;
    }

    public void setJob(Job job) {
        this.job = job;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setPlanProject(PlanProject planProject) {
        this.planProject = planProject;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public void setStoreCode(String storeCode) {
        this.storeCode = storeCode;
    }

    public void setStoreName(String storeName) {
        this.storeName = storeName;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public void setJobId(Long jobId) {
        this.jobId = jobId;
    }

    public void setPriority(int priority) {
        this.priority = priority;
    }

    //</editor-fold>
}
